/*****************************************************************************
 * plum_kernel_device.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/

#include "kernel/include/plum_kernel_api.h"
#include <string.h>

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "device"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define PLUM_DEV_COUNT \
    PLUM_SECTION_ITEM_COUNT(PLUM_DEVICE_SECTION, plum_device_t)
#define PLUM_DEV_GET(i) \
    PLUM_SECTION_ITEM_GET(PLUM_DEVICE_SECTION, plum_device_t, i)

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_SECTION_REF(PLUM_DEVICE_SECTION, plum_device_t);

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

/**
 * @brief: Find a device object handle by name.
 *
 * @param[in]: dev_name - The Device name.
 *
 * @return: Device object handle, otherwise NULL to fail.
 */
PLUM_PRIVATE
plum_device_ref_t plum_device_find(PLUM_CONST plum_s8 *dev_name)
{
    size_t loop;

    plum_u32 len = strlen((const char *)dev_name);

    for (loop = 0; loop < PLUM_DEV_COUNT; loop++) {
        plum_device_ref_t dev = PLUM_DEV_GET(loop);
        if (strlen((const char *)dev->dev_name) == len &&
            !strcmp((const char *)dev_name, (const char *)dev->dev_name)) {
            return dev;
        }
    }

    return NULL;
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_device_init(PLUM_CONST plum_s8 *dev_name)
{
    plum_device_ref_t dev = plum_device_find(dev_name);

    if (dev && dev->dev_api && dev->dev_api->init) {
        return (dev->dev_api->init(dev));
    }

    return (PLUM_ECODE_EIO);
}

PLUM_PUBLIC
plum_s32 plum_device_fini(PLUM_CONST plum_s8 *dev_name)
{
    plum_device_ref_t dev = plum_device_find(dev_name);

    if (dev && dev->dev_api && dev->dev_api->fini) {
        return (dev->dev_api->fini(dev));
    }

    return (PLUM_ECODE_EIO);
}

PLUM_PUBLIC
plum_s32 plum_device_reset(PLUM_CONST plum_s8 *dev_name)
{
    plum_device_ref_t dev = plum_device_find(dev_name);

    if (dev && dev->dev_api && dev->dev_api->reset) {
        return (dev->dev_api->reset(dev));
    }

    return (PLUM_ECODE_EIO);
}

PLUM_PUBLIC
plum_s32 plum_device_setpm(PLUM_CONST plum_s8 *dev_name, const plum_dev_pm_t pm)
{
    plum_device_ref_t dev = plum_device_find(dev_name);

    if (dev && dev->dev_api && dev->dev_api->setpm) {
        return (dev->dev_api->setpm(dev, pm));
    }

    return (PLUM_ECODE_EIO);
}

PLUM_PUBLIC
plum_s32 plum_device_read(PLUM_CONST plum_s8 *dev_name, plum_dev_read_t *arg)
{
    plum_device_ref_t dev = plum_device_find(dev_name);

    if (dev && dev->dev_api && dev->dev_api->read) {
        return (dev->dev_api->read(dev, arg));
    }

    return (PLUM_ECODE_EIO);
}

PLUM_PUBLIC
plum_s32 plum_device_write(PLUM_CONST plum_s8 *dev_name, plum_dev_write_t *arg)
{
    plum_device_ref_t dev = plum_device_find(dev_name);

    if (dev && dev->dev_api && dev->dev_api->write) {
        return (dev->dev_api->write(dev, arg));
    }

    return (PLUM_ECODE_EIO);
}

PLUM_PUBLIC
plum_s32 plum_device_ioctrl(PLUM_CONST plum_s8 *dev_name,
                            plum_dev_ioctrl_t * arg)
{
    plum_device_ref_t dev = plum_device_find(dev_name);

    if (dev && dev->dev_api && dev->dev_api->ioctrl) {
        return (dev->dev_api->ioctrl(dev, arg));
    }

    return (PLUM_ECODE_EIO);
}

PLUM_PUBLIC
plum_void plum_hal_device_init(plum_void)
{
    size_t         i;
    plum_device_t *item = NULL;
    for (i = 0; i < PLUM_DEV_COUNT; i++) {
        item = PLUM_DEV_GET(i);
        LOG_I("item name : %s", item->dev_name);
        if (item && item->dev_api && item->dev_api->init) {
            LOG_D("find dev : %s", item->dev_name);
            LOG_D("DEV API  : %p", item->dev_api);
            LOG_D("dev init : %p", item->dev_api->init);
            item->dev_api->init(item);
        }
    }
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
